﻿using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using MugenInjection.Infrastructure;
using MugenInjection.Interface;
using MugenInjection.Logging.Interface;

// ReSharper disable CheckNamespace

namespace MugenInjection
// ReSharper restore CheckNamespace
{
    /// <summary>
    /// Extension for <see cref="IInjector"/> add log support.
    /// </summary>
    public static class LoggingExtension
    {
        /// <summary>
        /// Gets the logger for the class calling this method.
        /// </summary>
        /// <returns>The newly-created logger.</returns>
        [MethodImpl(MethodImplOptions.NoInlining)]
        
        public static ILogger GetLogger(this IInjectorResolver injectorResolver)
        {
#if SIMPLESTACKFRAME
            MethodBase methodBase = new StackFrame(1).GetMethod();
#else
            MethodBase methodBase = new StackFrame(1, false).GetMethod();
#endif
            if (methodBase == null || methodBase.DeclaringType == null)
                throw new NullReferenceException("It is impossible to determine a class called method.");
            return (ILogger) injectorResolver.GetInto(typeof (ILogger), methodBase.DeclaringType);
        }

        /// <summary>
        /// Gets the logger for the class for the specified type.
        /// </summary>
        /// <returns></returns>
        
        public static ILogger GetLogger(this IInjectorResolver injectorResolver,  Type typeInto)
        {
            Validate.ArgumentNotNull(typeInto, "typeInto");
            return (ILogger) injectorResolver.GetInto(typeof (ILogger), typeInto);
        }
    }
}